﻿#region using statements

using System;
using System.Collections.Generic;
using System.Reflection;

#endregion

namespace XNAGameEngine.Utility
{
    /// <summary>
    /// This generic class allows "foreach" on enumerators
    /// example: foreach (EnumType enumValue in EnumParse.GetEnumValues<EnumType>())
    /// </summary>
    internal class EnumParse
    {
        public static List<T> GetEnumValues<T>()
        {
            Type currentEnum = typeof(T);
            List<T> resultSet = new List<T>();

            if (currentEnum.IsEnum)
            {
                FieldInfo[] fields =
                    currentEnum.GetFields(
                        BindingFlags.Static | BindingFlags.Public);
                foreach (FieldInfo field in fields)
                    resultSet.Add((T)field.GetValue(null));
            }
            else
                throw new ArgumentException(
                    "The argument must of type Enum or of a type derived from Enum",
                    "T");

            return resultSet;
        }
    }
}